package com.ljg.activiti.j_reveivetask;

import java.io.InputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 接受任务测试类
 * @author guo
 *
 */
public class ReceiveTaskTest {
	
	private Logger log = LoggerFactory.getLogger(ReceiveTaskTest.class);
	
	// 流程引擎对象
	private ProcessEngine processEngine;
	
	@Before
	public void before() {
		/**
		 * 创建默认的流程引擎对象，默认读取classpath下的activiti.cfg.xml创建流程引擎对象
		 */
		processEngine = ProcessEngines.getDefaultProcessEngine();
		log.info("创建流程引擎对象成功！");
	}
	
	/**
	 * 部署流程定义-从inputstream
	 *  processEngine.getRepositoryService()：获取流程定义和流程部署相关的Service
	 */
	@Test
	public void deploymentProcessDefinition_inputstream() {
		InputStream bpmnIn = this.getClass().getClassLoader().getResourceAsStream("diagrams/ReceiveTaskProcess.bpmn");
		InputStream pngIn = this.getClass().getClassLoader().getResourceAsStream("diagrams/ReceiveTaskProcess.png");
		Deployment deployment = processEngine.getRepositoryService() // 获取流程定义和流程部署相关的Service
			.createDeployment() // 创建流程部署对象
			.addInputStream("ReceiveTaskProcess.bpmn", bpmnIn) // 从InputStream中加载流程资源
			.addInputStream("ReceiveTaskProcess.png", pngIn) // 
			.name("ReceiveTaskProcess接收任务测试流程") // 添加部署的名称
			.deploy();
		
		log.info("部署完成，deploymentId=" + deployment.getId() + "，deploymentName=" + deployment.getName() + "，deploymentTime=" + deployment.getDeploymentTime());
	}
	
	/**
	 * 启动流程实例
	 * processEngine.getRuntimeService() // 获得正在执行的流程实例和执行对象相关的Service
	 */
	@Test
	public void startProcessInstance() {
		String processDefinitionKey = "ReceiveTaskProcess"; // 流程定义的key对应bpmn文件中id的属性值
		ProcessInstance pi = processEngine.getRuntimeService() // 获得与正在执行的流程实例和执行对象相关的Service
			.startProcessInstanceByKey(processDefinitionKey); // 根据流程定义的key启动流程实例，默认是按照最新版本的流程定义启动
		log.info("启动流程成功，Id=" + pi.getId() + "，deploymentId=" + pi.getDeploymentId() + "，name=" + pi.getName() + "，processInstanceId=" + pi.getProcessInstanceId());
		
		// 查询执行对象Id
		String activityId = "receivetask1";
		Execution e1 = processEngine.getRuntimeService()//
			.createExecutionQuery() // 创建执行对象查询
			.processInstanceId(pi.getId()) // 根据流程实例Id查询
			.activityId(activityId) // 当前活动Id，对应bpmn文件中的活动的定义Id的值
			.singleResult();
		
		// 使用流程变量来设置业务销售额，用来传递业务数据
		processEngine.getRuntimeService()//
			.setVariable(e1.getId(), "money", 10000);
		
		// 流程前进一步，向后执行一步
		processEngine.getRuntimeService()//
			.signal(e1.getId());
		
		String activityId2 = "receivetask2";
		Execution e2 = processEngine.getRuntimeService()//
			.createExecutionQuery() // 创建执行对象查询
			.processInstanceId(pi.getId()) // 根据流程实例Id查询
			.activityId(activityId2) // 当前活动Id，对应bpmn文件中的活动的定义Id的值
			.singleResult();
		
		// 获取流程变量的值，获取当日销售额
		Integer money = (Integer) processEngine.getRuntimeService()//
			.getVariable(e2.getId(), "money");
		
		System.out.println("老总您好，我们部门的今日销售额为：" + money);
		
		// 流程向后执行一步
		processEngine.getRuntimeService()//
		.signal(e2.getId());
		
	}

}
